package com.njlime.sys.repository;

import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.entities.Menu;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-7-18.
 */
@Repository
public class MenuDaoImpl extends BaseDaoImpl<Menu> {

    /**
     * 根据用户ID获取菜单列表--用户权限
     *
     * @param userId
     * @return menu列表
     */
    public List<Menu> listByUserId(Integer userId) {
        List<Menu> menuList = null;
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select distinct t.* from t_menu t ");
        sb.append("join t_role_menu rm on rm.menu_id = t.id ");
        sb.append("join t_role r on r.id = rm.role_id ");
        sb.append("join t_user_role ur on ur.role_id = r.id ");
        sb.append("join t_user u on u.id = ur.user_id ");
        sb.append("where t.is_del = 0 and t.status = 1 and r.is_del = 0 and u.is_del = 0 and  u.id = ? ");
        arrList.add(userId);
        sb.append("order by t.sort ");
        menuList = findList(sb.toString(), arrList);
        return menuList;
    }

    /**
     * 获取全部菜单列表--用户权限
     *
     * @return menu列表
     */
    public List<Menu> findAllList() {
        List<Menu> menuList = null;
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_menu t where is_del = 0 order by t.sort");
        menuList = findList(sb.toString(), arrList);
        return menuList;
    }

    /**
     * 根据id获取菜单信息
     *
     * @return
     */
    public Map<String,Object> getById(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.*,p.name parent_name from t_menu t left join t_menu p on p.id = t.parent_id where t.id = ? and t.is_del = 0 ");
        arrList.add(id);
        List<Map<String,Object>> menuList = getListJdbc(sb.toString(), arrList);
        if(menuList != null && menuList.size() > 0){
            return menuList.get(0);
        }
        return null;
    }

    /**
     * 根据条件获取菜单列表分页数据
     *
     * @param pageBean
     * @param menu
     * @return MapList
     */
    public PageBean list(PageBean pageBean, Menu menu) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_menu t ");
        sb.append("where is_del = 0 ");
        if (!StringUtil.isEmpty(menu.getName())) {
            sb.append("and name like concat('%',?,'%')");
            arrList.add(menu.getName());
        }
        if (menu.getParent_id() != null) {
            sb.append("and parent_id = ? ");
            arrList.add(menu.getParent_id());
        }
        if (menu.getIs_show() != null) {
            sb.append("and is_show = ? ");
            arrList.add(menu.getIs_show());
        }
        if (menu.getStatus() != null) {
            sb.append("and status = ? ");
            arrList.add(menu.getStatus());
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        pageBean.setTotalPage(pageBean.getTotalPage());
        if (num > 0) {
            sb.append(" order by t.sort desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 编辑菜单
     *
     * @param menu
     * @return
     */
    public int updateMenu(Menu menu) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_menu set update_time = ?,updater = ? ");
        arrList.add(menu.getUpdate_time());
        arrList.add(menu.getUpdater());
        if (!StringUtil.isEmpty(menu.getName())) {
            sb.append(",name = ? ");
            arrList.add(menu.getName());
        }
        if (!StringUtil.isEmpty(menu.getIcon())) {
            sb.append(",icon = ? ");
            arrList.add(menu.getIcon());
        }
        if (!StringUtil.isEmpty(menu.getIcon_hover())) {
            sb.append(",icon_hover = ? ");
            arrList.add(menu.getIcon_hover());
        }
        if (menu.getParent_id() != null) {
            sb.append(",parent_id = ? ");
            arrList.add(menu.getParent_id());
        }
        if (!StringUtil.isEmpty(menu.getParent_ids())) {
            sb.append(",parent_ids = ? ");
            arrList.add(menu.getParent_ids());
        }
        if (!StringUtil.isEmpty(menu.getPermission())) {
            sb.append(",permission = ? ");
            arrList.add(menu.getPermission());
        }
        if (!StringUtil.isEmpty(menu.getTemplate())) {
            sb.append(",template = ? ");
            arrList.add(menu.getTemplate());
        }
        if (!StringUtil.isEmpty(menu.getUrl())) {
            sb.append(",url = ? ");
            arrList.add(menu.getUrl());
        }
        if (menu.getIs_show() != null) {
            sb.append(",is_show = ? ");
            arrList.add(menu.getIs_show());
        }
        if (menu.getStatus() != null) {
            sb.append(",status = ? ");
            arrList.add(menu.getStatus());
        }
        if (menu.getSort() != null) {
            sb.append(",sort = ? ");
            arrList.add(menu.getSort());
        }
        sb.append(" where id = ?");
        arrList.add(menu.getId());
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 获得最大排序值
     * @return
     */
    public int getMaxSort(){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select IFNULL(max(sort),0) sort from t_menu ");
        Map<String,Object> map = getListJdbc(sb.toString(),arrList).get(0);
        return Integer.parseInt(map.get("sort").toString());
    }

    /**
     * 寻找下一个排序菜单
     * @param id
     * @return
     */
    public Menu getNextSortMenu(Integer id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_menu t ");
        sb.append("join (select parent_id,sort from t_menu where id = ? ) m on m.parent_id = t.parent_id ");
        sb.append("where t.sort > m.sort ");
        sb.append("order by t.sort asc limit 1");
        arrList.add(id);
        int num = getListJdbcSize(sb.toString(), arrList);
        if(num > 0){
            return findList(sb.toString(),arrList).get(0);
        }
        return null;
    }

    /**
     * 寻找上一个排序菜单
     * @param id
     * @return
     */
    public Menu getBeforeSortMenu(Integer id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_menu t ");
        sb.append("join (select parent_id,sort from t_menu where id = ? ) m on m.parent_id = t.parent_id ");
        sb.append("where t.sort < m.sort ");
        sb.append("order by t.sort desc limit 1");
        arrList.add(id);
        int num = getListJdbcSize(sb.toString(), arrList);
        if(num > 0){
            return findList(sb.toString(),arrList).get(0);
        }
        return null;
    }

    /**
     * 根据父id获取子级菜单的数量
     *
     * @param parent_id
     * @return
     */
    public int countByParentId(Integer parent_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select id from t_menu where parent_id = ? and is_del = 0 ");
        arrList.add(parent_id);
        return getListJdbcSize(sb.toString(), arrList);
    }

    /**
     * 删除单位
     *
     * @param id
     * @return
     */
    public int deleteMenu(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_menu set is_del = 1 where id = ? ");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 根据角色获取菜单
     *
     * @param role_id
     * @return
     */
    public List<Map<String, Object>> getMenuByRole(Integer role_id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_menu t ");
        sb.append("join t_role_menu rm on  rm.menu_id = t.id ");
        sb.append("join t_role r on r.id = rm.role_id ");
        sb.append("where t.is_del = 0 and r.is_del = 0 and r.id = ? ");
        arrList.add(role_id);
        return getListJdbc(sb.toString(), arrList);
    }

    /**
     * 根据模板名称获取权限
     * @param template
     * @return
     */
    public Menu getByTemplate(String template){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_menu t where template = ? ");
        arrList.add(template);
        return findOne(sb.toString(),arrList);
    }

    /**
     * 根据url获取权限
     * @param url
     * @return
     */
    public Menu getByUrl(String url){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_menu t where url = ? ");
        arrList.add(url);
        return findOne(sb.toString(),arrList);
    }


    /**
     * 获取当前id的菜单的所有子菜单（包含本身）
     * @param id
     * @return
     */
    public List<Menu> getMenuChildList(int id){
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select t.* from t_menu t where t.id = ? ");
        arrList.add(id);
        Menu menu = findOne(sb.toString(), arrList);
        if(menu != null){
            sb.setLength(0);
            arrList.clear();
            sb.append("select * from t_menu where parent_ids like concat(?,'%')");
            arrList.add(menu.getParent_ids() + menu.getId() + ",");
            return findList(sb.toString(),arrList);
        }else{
            return null;
        }
    }

}
